package com.liuhaoqi.raletiveblog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuhaoqi.raletiveblog.entity.Resource;
import com.liuhaoqi.raletiveblog.mapper.ResourceMapper;
import com.liuhaoqi.raletiveblog.result.BlogPageResult;
import com.liuhaoqi.raletiveblog.result.WebRestResult;
import com.liuhaoqi.raletiveblog.result.business.PageResult.ResourcePageResult;
import com.liuhaoqi.raletiveblog.service.ResourceService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author liuhaoqi
 * @Date 2020/4/21 10:55
 * @Version 1.0
 */
@Controller
public class ResourceController {
    @Autowired
    ResourceService resourceService;

    @GetMapping("/resource")
    public String getResourceList(Model model,
                                  @RequestParam(value = "pageNum",defaultValue = "1") String pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "8") String pageSize,
                                  @RequestParam(value = "resourcetype",required = false) String resourcetype) {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<Resource> resourceList = resourceService.getResourceList();
        PageInfo<Resource> resourceListPageInfo = new PageInfo<>(resourceList);
        model.addAttribute("resources",resourceListPageInfo);
        if (resourcetype != null) {
            PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
            List<Resource> resourceByTypeList = resourceService.getResourceByTypeList(resourcetype);
            PageInfo<Resource> resourceByTypeListPageInfo = new PageInfo<>(resourceByTypeList);
            model.addAttribute("resourceByTypePageInfo",resourceByTypeListPageInfo);
        }
        //根据资源类别找出该资源
        return "resource";
    }

//    @GetMapping("/getAllResource")
//    @ResponseBody
//    public PageInfo getResourceTypeList(@RequestParam("resourceTypeName") String resourcetype,Model model,
//                                      @RequestParam(value = "pageNum",defaultValue = "1") String pageNum,
//                                      @RequestParam(value = "pageSize",defaultValue = "8") String pageSize){
//        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
//        List<Resource> resourceByTypeList = resourceService.getResourceByTypeList(resourcetype);
//        PageInfo<Resource> resourceByTypeListPageInfo = new PageInfo<>(resourceByTypeList);
//        return resourceByTypeListPageInfo;
//    }

    @GetMapping("/admin/getResourceListMang")
    @ResponseBody
    public BlogPageResult<ResourcePageResult> getResourceListMang(@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                                                              @RequestParam(defaultValue = "10",value = "pageSize") int pageSize) {
        int start=(pageNum-1)*pageSize;
        int end=pageSize;
        List<ResourcePageResult> resourceListMang = resourceService.getResourceListMang(start, end, pageSize);
        BlogPageResult<ResourcePageResult> result = new BlogPageResult<>();
        result.setCount(resourceListMang.get(0).getCnt());
        result.setCode(0);
        result.setData(resourceListMang);
        result.setMsg("查询资源信息成功！");
        return result;
    }

    @PostMapping("/admin/deleteResourceByResourceId")
    @ResponseBody
    public WebRestResult deleteResourceByResourceId(@RequestParam("resourceId") Long resourceId) {
        WebRestResult result = new WebRestResult();
        int i = resourceService.deleteResourceByResourceId(resourceId);
        if (i > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除资源成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除资源失败！");
        }
        return result;
    }

    @PostMapping("/admin/updateResourceByResourceId")
    @ResponseBody
    public WebRestResult updateResourceByResourceId(@RequestBody Resource resource) {
        WebRestResult result = new WebRestResult();
        int i = resourceService.updateResourceByResourceId(resource);
        if (i > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("更新资源成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
        result.setMessage("更新资源失败  ！");
        }
        return result;
    }

    @PostMapping("/admin/addResource")
    @ResponseBody
    public WebRestResult addResource(@RequestBody Resource resource) {
        WebRestResult result = new WebRestResult();
        int i = resourceService.addResource(resource);
        if (i > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("增加资源成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("增加资源失败！");
        }
        return result;
    }

}
